#include "GY_GradientComboBox.h"
#include <QDebug>
#include <QColor>

GY_GradientComboBox::GY_GradientComboBox(QWidget *parent)
    : QComboBox(parent)
{

}

void GY_GradientComboBox::addGradientDataItem(const QString &GradientName, GradientData* gradientData)
{
    if(GradientName.isNull() || gradientData==nullptr)
    {
        return;
    }

    QLabel* pLabel = addGradientWidget(gradientData);
    pLabel->setFixedSize(20,20);
    QPixmap pixmap = pLabel->grab(pLabel->rect()); //获取图形截图
    this->addItem(QIcon(pixmap),GradientName);
    this->setSizeAdjustPolicy(QComboBox::AdjustToContents);
}

QLabel *GY_GradientComboBox::addGradientWidget(GradientData *gradientData)
{
    QLabel* pLabel = new QLabel();
    QString extend;
    if(gradientData->extend=="0")
    {
        extend = "pad";
    }
    else if(gradientData->extend=="1")
    {
        extend = "reflect";
    }
    else if(gradientData->extend=="2")
    {
        extend = "repeat";
    }

    QString x1 = QString::number(gradientData->startpoint_x.toDouble()/256,'f',5);
    QString y1 = QString::number(gradientData->startpoint_y.toDouble()/256,'f',5);
    QString x2 = QString::number(gradientData->endpoint_x.toDouble()/256,'f',5);
    QString y2 = QString::number(gradientData->endpoint_y.toDouble()/256,'f',5);
    QString startradius = QString::number(gradientData->startradius.toDouble()/256,'f',5);
    QString endradius = QString::number(gradientData->endradius.toDouble()/256,'f',5);
    QString startcolor = " rgba("+ gradientData->startcolor_r + "," + gradientData->startcolor_g + "," + gradientData->startcolor_b + "," + gradientData->startcolor_a + ")";
    QString endcolor = " rgba(" + gradientData->endcolor_r + "," + gradientData->endcolor_g + "," + gradientData->endcolor_b + "," + gradientData->endcolor_a + ")";

    if(gradientData->radial=="0") //直线渐变
    {
        pLabel->setStyleSheet("QLabel{background-color: qlineargradient(spread:"+ extend+ ", x1:" + x1+ ", y1:" + y1+ ", x2:" + x2+ ", y2:" + y2+ ","
                              "stop:0 "+startcolor+", stop:1 " +endcolor+ ");}");
    }
    else //环形渐变
    {
        if(gradientData->name.contains("2"))
        {
            pLabel->setStyleSheet("QLabel{background-color: qradialgradient(spread:"+ extend+ ", cx:" + x1+ ", cy:" + y1+ ", radius:"+startradius+", fx:" + x2+ ", fy:" + y2+ ","
                                  "stop:"+ endradius + endcolor +",stop:1" + startcolor + ");}");
        }
        else {
            pLabel->setStyleSheet("QLabel{background-color: qradialgradient(spread:"+ extend+ ", cx:" + x1+ ", cy:" + y1+ ", radius:"+startradius+", fx:" + x2+ ", fy:" + y2+ ","
                                  "stop:"+ endradius + startcolor +", stop:1" + endcolor + ");}");
        }
    }
    return pLabel;
}
